package com.example.service.impl;

import cn.hutool.core.util.StrUtil;
import com.example.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.example.entity.TbSpecification;
import com.example.entity.TbSpecificationExample;
import com.example.mapper.TbSpecificationMapper;
import com.example.service.TbSpecificationService;

import com.example.vo.TbSpecificationVO;
import com.example.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TbSpecificationServiceImpl implements TbSpecificationService {
	@Autowired
	private TbSpecificationMapper tbSpecificationMapper;

	public PageResult<TbSpecification> search(Integer page, Integer pageSize, TbSpecificationVO tbSpecificationVO) {
		// 1. 启动分页查询
		PageHelper.startPage(page, pageSize);
		// 2. 构造查询条件
		TbSpecificationExample example = new TbSpecificationExample();
		TbSpecificationExample.Criteria criteria = example.createCriteria();
		if (StrUtil.isNotBlank(tbSpecificationVO.getSpecName())) {
			criteria.andSpecNameLike("%" + tbSpecificationVO.getSpecName() + "%");
		}
		List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(example);
		Page<TbSpecification> studentPage = (Page<TbSpecification>) tbSpecifications;
		return new PageResult<>(studentPage.getResult(),studentPage.getTotal());
	}
	public boolean save(TbSpecification tbSpecification) {
		return tbSpecificationMapper.insert(tbSpecification) > 0;
	}
}